#include <allegro.h>
#include <stdio.h>
#include "fight.h"

#define NB_IMAGES 3
#define NB_IMAGES_FIGHT 2 // pr pouvoir charger uniquement les images 1 et 2

// load MB & Richard 

void load_MB(MB *mini)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "../images/right%d.bmp", i);
		mini->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "../images/left%d.bmp", i);
		mini->img_left[i] = load_bitmap(buf, NULL);
	}
	
mini->img_face=load_bitmap("../images/face.bmp", NULL);

}




void load_R(richard *r)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "../images/r%d.bmp", i);
		r->img_r[i] = load_bitmap(buf, NULL);
		sprintf(buf, "../images/l%d.bmp", i);
		r->img_l[i] = load_bitmap(buf, NULL);
	}

}



// Destroy + init MB

void destroy_MB(MB *mini)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) {
		destroy_bitmap(mini->img_right[i]);
		destroy_bitmap(mini->img_left[i]);
	}
destroy_bitmap(mini->img_face);

}

void init_MB(MB *mini, int H, int W)
{
	mini->direction = 0;
	mini->x = 520;
	mini->y =600;
	mini->W=WIDTH; // valeur cte (.h)
	mini->H=HIGHT; // valeur cte (.h)
	mini->coll=0;
	mini->agg=0;
	mini->is_fighting=0; //etat de fight
	mini->is_alive=1; //vie
	mini->HP_MAX=1200;
	mini->HP=1200;
	mini->DPS_MIN=200;
	mini->DPS_MAX=425;
	mini->HC=55
}


// Destroy + init Richard

void destroy_R(richard *r)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) {
		destroy_bitmap(r->img_l[i]);
		destroy_bitmap(r->img_l[i]);
	}

}


void init_R(richard *r,int H1, int W1)
{
	r->direction = 0;
	r->x = 0;
	r->y =600;
	r->w=W1;
	r->h=H;
	r->coll=0;
	r->agg=0;//ditance entre richard et le pirate pr que le pirate s'approche de lui
	r->is_fighting=0; //etat de fight
	r->is_alive=1; //vie
	r->HP_MAX=10000;
	r->HP=10000;
	r->DPS_MIN=20;
	r->DPS_MAX=100;
	r->HC=70;
}




//Background


BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	return (Background);

}



void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}



void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}




void move_MB_right(MB *mini,BITMAP * Background)
{  

	if ( (mini->x) < (SCREEN_W - mini->img_right[0]->w) && (mini->direction)==0 )
	 {

		(mini->x)+=1;
		usleep(4000);
		mini->direction=0;
	}

		if( (mini->x) == SCREEN_W - mini->img_right[0]->w )
		{mini->direction = 1;} //changer vers la gauche
}


void move_MB_left(MB *mini, BITMAP *Background)
{

	if  (mini->x > 0 + mini->img_left[1]->w && mini->direction==1  ) 
	{

	(mini->x)-=1;
	usleep(4000);
	mini->direction=1;

	}

	if  ( (mini->x) == ( 0 + mini->img_left[1]->w))
	{mini->direction = 0;} // changer vers la droite
}




void mouvement_richard(richard *r,BITMAP *Background,BITMAP*Buffer)
{

int i,j;

if (key[KEY_Q])
{
r->direction=0;
r->x--;
/*if (r->direction==0)
{

//draw_sprite(Buffer, r->img_l[1], r->x, r->y);  // enlève le rose du fond

//draw_sprite(Buffer, r->img_l[1], r->x, r->y);  // enlève le rose du fond
//blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // garde le rose du fond
//blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);  

}*/
}

if (key[KEY_D])
{
r->direction=1;
r->x++;
if (r->direction==1)
{

//draw_sprite(Buffer, r->img_r[1], r->x, r->y);
//blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
//blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);

}
}


}


// draw MB & Richard

void draw_MB(MB mini,BITMAP *Buffer, int compteur)
{
	static int last_direction;// entier statique (qui ne change pas, allocation reste tjrs même si on sort de la fct)

	if (mini.direction == 0)//moving right
	{ 
		draw_sprite(Buffer, mini.img_right[compteur % (NB_IMAGES-1) ],mini.x, mini.y);
		last_direction=0;
	}

	if(mini.direction == 1) //moving left
	{
		draw_sprite(Buffer, mini.img_left[compteur % (NB_IMAGES-1) ],mini.x, mini.y);
		last_direction=1;
	}

}



void draw_R(richard r,BITMAP *Buffer, int compteur)
{
	static int last_direction;// entier statique (qui ne change pas, allocation reste tjrs même si on sort de la fct)

	if (r.direction == 0)//moving right
	{ 
		draw_sprite(Buffer, r.img_l[compteur % (NB_IMAGES-1) ],r.x, r.y);
		last_direction=0;
	}

	if(r.direction == 1) //moving left
	{
		draw_sprite(Buffer, r.img_r[compteur % (NB_IMAGES-1) ],r.x, r.y);
		last_direction=1;
	}

}


int Collision(richard r, MB mini)
{
	if((mini.x > r.x + r.w) || (mini.x + mini.w < r.x)) //ils sont loins l'un de l'autre=> mini marche aléa
		return 0;
	else
		return 1;// sinon ils sont proches et on lance le fight
}


void fight(richard *r, MB *mini)
{

	int nb = rand() % 100;

	if(mini->is_fighting==0)
	{
		if(nb<=mini->HC) // il y a une chance qu'un coup fonctionne ( val de HC fixée à 55 pr MB).
		{
		nb = (rand() % (mini->DPS_MAX - mini->DPS_MIN))+mini->DPS_MIN; /* nb = ( gd nbre aléatoire ) % (nbre entre DPS_MAX et DPS_MIN) 
on aura un petit nbre. On lui ajoute valeur de mini->DPS_MIN et on aura finalement une valeur entre DPS_MAX et DPS_MIN */

		r->HP=r->HP-nb; // pts de vie de Richard diminuent*/
		}

		if(r->HP<=0)
		{
			r->is_alive=0;
		}
		mini->is_fighting=1;
	}
}

void main_fight(richard *r, MB *mini)
{

	if(r->is_alive==1)
	{
			if (key[KEY_ENTER]&& Collision((*r),mini)!=0)
			{
			fight(&r,&mini);				
			}
	}
}


